"""
@File    :   models.py
@Time    :   2024/02/22 22:16:15
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   系统配置数据模型
"""

from datetime import datetime

from sqlalchemy import JSON, BigInteger, Column, DateTime, Integer, String

from src.business.admin.system.settings.enums import (
    LoginFailureLockoutPolicyEnum,
)
from src.config import get_app_settings
from src.models.models import OrmBase

from .enums import PasswordStrengthEnum

# 获取应用配置
settings = get_app_settings()


class SystemSettings(OrmBase):
    """系统设置数据模型"""

    __tablename__ = "system_setting"

    id = Column(BigInteger(), primary_key=True, autoincrement=True, comment="主键")
    min_password_length = Column(
        Integer(), default=8, nullable=False, comment="密码最小长度"
    )
    max_password_length = Column(
        Integer(), default=20, nullable=False, comment="密码最大长度"
    )
    password_strength = Column(
        String(20),
        default=PasswordStrengthEnum.high.value,
        nullable=False,
        comment="密码强度要求",
    )
    login_failure_lockout_policy = Column(
        String(50),
        default=LoginFailureLockoutPolicyEnum.lock_username.value,
        nullable=False,
        comment="登录失败锁定策略",
    )
    login_failure_lockout_times = Column(
        Integer(), default=10, nullable=False, comment="登录失败锁定次数"
    )
    login_failure_lock_minutes = Column(
        Integer(), default=5, nullable=False, comment="登录失败锁定时间(分钟)"
    )
    # 文件上传相关
    user_avatar_image_upload_max_size = Column(
        Integer(),
        default=settings.default_upload_avatar_max_size,
        nullable=False,
        comment="上传用户头像的最大大小(单位: MB)",
    )
    user_avatar_image_upload_type = Column(
        JSON(),
        default=settings.default_allow_upload_avatar_suffix,
        nullable=False,
        comment="允许上传的用户头像图片格式",
    )
    image_upload_max_size = Column(
        Integer(),
        default=settings.default_upload_image_max_size,
        nullable=False,
        comment="上传图片的最大大小(单位: MB)",
    )
    image_upload_type = Column(
        JSON(),
        default=settings.default_allow_upload_image_suffix,
        nullable=False,
        comment="允许上传的图片格式",
    )
    video_upload_max_size = Column(
        Integer(),
        default=settings.default_upload_video_max_size,
        nullable=False,
        comment="上传视频的最大大小(单位: MB)",
    )
    video_upload_type = Column(
        JSON(),
        default=settings.default_allow_upload_video_suffix,
        nullable=False,
        comment="允许上传的视频格式",
    )
    file_upload_max_size = Column(
        Integer(),
        default=settings.default_upload_file_max_size,
        nullable=False,
        comment="上传文件的最大大小(单位: MB)",
    )
    file_upload_type = Column(
        JSON(),
        default=settings.default_allow_upload_file_suffix,
        nullable=False,
        comment="允许上传的文件格式",
    )
    create_datetime = Column(
        DateTime(), nullable=False, default=datetime.now, index=True, comment="创建时间"
    )
    update_datetime = Column(
        DateTime(),
        nullable=False,
        default=datetime.now,
        onupdate=datetime.now,
        index=True,
        comment="修改时间",
    )
    # 创建者用户名(级联删除)
    creator_username = Column(
        String(20), default="", nullable=False, comment="创建者用户名"
    )
    # 修改者用户名(级联删除)
    updater_username = Column(
        String(20), default="", nullable=False, comment="修改者用户名"
    )
    # 租户ID(级联删除)
    tenant_id = Column(BigInteger(), nullable=False, unique=True, comment="租户ID")
